﻿using System;
using Framework.MVC;
using Account.Model;
using Account.Model.Behavior;
using Identity.Model;
using Framework.MVVM.Model;
using Identity.IService;
using Identity.Service;
using Account.IService;
using Account.Service;


namespace FrontCenter.Models
{
    [ServiceAttribute(typeof(IVerifyCodeService), typeof(VerifyCodeService), "Instance")]
    [ServiceAttribute(typeof(IMobileService), typeof(MobileService), "Instance")]
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    public class ValidateMobileModel : AsyncModelBase, IValidateIdentity,IDeleteIdentity,IValidateVerifyCode, IDeleteVerifyCode,IGetMobile,INewIdentity
    {
        #region Field

        private const string m_error = "error";

        #endregion Field

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();

            this.ValidateIdentity("getpassword", string.Empty, string.Empty, string.Empty, string.Empty);
            IdentityResource = "getpassword";
        }

        #endregion AsyncModelBase

        #region IValidateIdentity

        public string IdentityCode { get; set; }

        public bool IsOnce { get; set; }

        public string Source { get; set; }

        public void OnValidateIdentityComplete(string key, Identity.IService.Entry.IdentityEntry entry)
        {
            AccountCode = entry.value;
            IdentityValue = entry.value;
            this.GetMobile("user",string.Empty,string.Empty);
        }

        public void OnValidateIdentityError(string key)
        {
            OnComplete(m_error, "identity");
            OnEndRequest();
        }

        #endregion IValidateIdentity

        #region IGetMobile

        public string AccountCode { get; set; }

        public void OnGetMobileComplete(string key, Account.IService.Entry.ValueEntity<string> entry)
        {
            VerifyCode = entry.value;
            this.ValidateVerifyCode("Mobile",string.Empty,string.Empty);
        }

        public void OnGetMobileError(string key)
        {
            OnComplete(m_error, "mobile");
            OnEndRequest();
        }

        #endregion IGetMobile

        #region IValidateVerifyCode

        public void OnValidateVerifyCodeComplete(string key, bool entry)
        {
            OnBeginRequest();
            IdentityResource = "modifypassword";
            this.NewIdentity(string.Empty, string.Empty, string.Empty, string.Empty);
            this.DeleteVerifyCode("Mobile",string.Empty,string.Empty);
        }

        public void OnValidateVerifyCodeError(string key)
        {
            OnComplete(m_error, "verifycode");
            OnEndRequest();
        }

        public string VerifyCode { get; set; }

        public string VerifyValue { get; set; }

        #endregion IValidateVerifyCode

        #region IDeleteVerifyCode

        public void OnDeleteVerifyCodeComplete(string key, bool result)
        {
            OnEndRequest();
        }

        public void OnDeleteVerifyCodeError(string key)
        {
            OnEndRequest();
        }

        #endregion IDeleteVerifyCode

        #region INewIdentity

        public string IdentityValue { get; set; }

        public void OnNewIdentityComplete(string key, Identity.IService.Entry.IdentityEntry entry)
        {
            OnComplete("entry", entry);
            IdentityResource = "getpassword";
            this.DeleteIdentity(string.Empty, string.Empty);
        }

        public void OnNewIdentityError(string key)
        {
            OnComplete(m_error, "newidentity");
            OnEndRequest();
        }

        #endregion INewIdentity

        #region IDeleteIdentity

        public string IdentityResource { get; set; }

        public void OnDeleteIdentityComplete(string key, Identity.IService.Entry.IdentityEntry entry)
        {
            OnEndRequest();
        }

        public void OnDeleteIdentityError(string key)
        {
            OnEndRequest();
        }

        #endregion IDeleteIdentity
    }
}